//
//  WCExtractAction.m
//  WebClipper
//
//  Created by Daniel Braun on 15/03/07.
//  Copyright 2007 Daniel Braun http://page-appart.net, http://braun.daniel.free.fr All rights reserved.
//

#import "WCExtractAction.h"
#import <objc/objc.h>
#import <objc/objc-runtime.h>

@protocol htmlExtThatMayNotBeImplemented
// just to avoid warning with NODOM
- (NSString *)innerHTML;
- (NSString *)innerText;
- (NSString *)outerHTML;
- (NSString *)outerText;
- (NSString *)nodeValue;

@end

@implementation WCExtractAction


+ (NSString *) description
{
	return @"should not be used";
}
+ (BOOL) canHandle:(id)item
{
	return YES;
}

- (void) dealloc {
	[_guiHandler release];
	[super dealloc];
}

-(NSString *) description
{
	NSString *s=[NSString stringWithFormat:@"* %@", [[self class]description]];
	return s;
}
- (id) handle:(DOMHTMLElement *)el fromUrl:(NSURL *)url
{
	id value=nil;
	if ([el respondsToSelector:@selector(outerHTML)]) value=[(id)el outerHTML];
	else if ([el respondsToSelector:@selector(nodeValue)]) {
		value=[(id)el nodeValue];
	}
	else if ([el respondsToSelector:@selector(stringValue)]) value=[(id)el stringValue];	
	return value;
}
- (void) setupGui;
{
	id gc;
	_guiInit=YES;
	NSString *gcName=[NSString stringWithFormat:@"%@_gui", [self className]];
	const char *gcs=[gcName UTF8String];
	gc=(id) objc_lookUpClass(gcs);
	if (!gc) return;
	_guiHandler=[[gc alloc]init];
	if (_guiHandler) {
		if ([_guiHandler respondsToSelector:@selector(setParentObject:)]) {
			[_guiHandler setParentObject:self];
		}
	}
}
- (id) guiHandler
{
	return _guiHandler;
}

- (BOOL) isNew
{
	return !_guiInit;
}
// transparently forward method to gui object
+(NSMethodSignature*)methodSignatureForSelector:(SEL)selector
{
	return [NSMutableArray methodSignatureForSelector:selector];
}

+(void)forwardInvocation:(NSInvocation*)invocation
{
	SEL selector;
	//id ret,obj;
	//NSString *selName;
	
	selector=[invocation selector];
	//class creation method
	[invocation invokeWithTarget:[NSMutableArray class]];
	//[invocation getReturnValue:&ret];
	//obj=[[MArray alloc] _init_];
	//[obj setProxiedObject:ret];
	//[invocation setReturnValue:&obj];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
	if ([_guiHandler respondsToSelector:[anInvocation selector]]) {
		[anInvocation invokeWithTarget:_guiHandler];
	} else {
		[super forwardInvocation:anInvocation];
	}
}

- (BOOL) respondsToSelector:(SEL)aSelector
{
	if ( [super respondsToSelector:aSelector] )
		return YES;
	else if ([_guiHandler respondsToSelector:aSelector]) {
		return YES;
	}
	return NO;
}

- (NSMethodSignature*) methodSignatureForSelector:(SEL)selector
{
	NSMethodSignature *sig;
	sig=[_guiHandler methodSignatureForSelector:selector];
	if (!sig) return [super methodSignatureForSelector:selector];
	return sig;
}



- (id) initWithCoder:(NSCoder *) coder
{
	//self=[super initWithCoder:coder];
	self=[super init];
	if ([coder allowsKeyedCoding]) {
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	//[super encodeWithCoder:coder];
	if ( [coder allowsKeyedCoding] ) {
	}
}

@end
